layui.use('form', function () {
    let form = layui.form();
    form.on('select(pwdVisitType)', function (data) {
        if (data.value == 2) {
            $("#optPwd").hide()
        } else {
            $("#optPwd").show()
        }
        console.log(data.value);

    });
});

//机器新增
$('#opt_build').click(function () {
    var opt_name = $('#opt_name').val()
    var opt_host = $('#opt_host').val()
    var opt_username = $('#opt_username').val()
    var pwd_type = $('#pwd_type').find('option:selected').val()
    var opt_password = $('#opt_password').val()
    var opt_addr = $('#opt_addr').val()
    var opt_desc = $('#opt_desc').val()

    var data = {"opt_name":opt_name,"opt_host":opt_host,"opt_username":opt_username,
                "pwd_type":pwd_type,"opt_password":opt_password,"opt_addr":opt_addr,
                "opt_desc":opt_desc
                }
    $.ajax({
        url: /opt-build-add/,
        type: 'POST',
        data: data,
        success: function (ret) {
            if (ret.code === 0) {

                //alert("ok")
                parent.layer.close(parent.layer.index);
                window.parent.location.reload();
                //layer.alert('新增成功', {icon: 5, time: 1000});
            }
            if (ret.code ===-1){
                alert(ret.msg)
            }

        }, error: function (ret) {
            console.error('fail')

        }
    });


})


//项目构建
$('#pro_build').click(function () {
    var git_addr = $('#git_addr').val();
    var build_way = $('#build_way').find('option:selected').val();
    var project_lan = $('#project_lan').find('option:selected').val();
    var project_name = $('#project_name').val();
    var log_path = $('#log_path').val();
    var install_type = $('#install_type').find('option:selected').val();
    var opt_id = $('#opt_id').find('option:selected').val();
    var project_desc = $('#project_desc').val();

    var data = {"git_addr":git_addr,"build_way":build_way,"project_lan":project_lan,"project_name":project_name,
                "log_path":log_path,"install_type":install_type,"opt_id":opt_id,"project_desc":project_desc
                }
    $.ajax({
        url: /project-build-add/,
        type: 'POST',
        data: data,
        success: function (ret) {
            if (ret.code === 0) {

                //alert("ok")
                parent.layer.close(parent.layer.index);
                window.parent.location.reload();
                //layer.alert('新增成功', {icon: 5, time: 1000});
            }
            if (ret.code ===-1){
                alert(ret.msg)
            }

        }, error: function (ret) {
            console.error('fail')

        }
    });

})


//部署
$('#deploy').click(function () {

    var pro_id = $('#projectName').find("option:selected").val()
    var pro_name = $('#projectName').find("option:selected").text()
    var desc = $('#dep_desc').val();

    var url2 = /project-action/

    var timer = 0;

    $.ajax({
        url: url2,
        type: 'POST',
        data: {"pro_id": pro_id},
        success: function (ret) {
            if (ret.code == 0) {
                console.log('ok')
                //clearInterval(timer);
            }

        }, error: function (ret) {
            console.error('fail')
            clearInterval(timer);
        }
    });


    var begin = 1;

    timer = setInterval(function () {

        if(begin > 2){
            console.info("begin count",begin)
            clearInterval(timer);
            console.info("time reach stop")
        }

        var url = /pull/;
        $.ajax({
            url: url,
            type: 'GET',
            data: {"pro_name": pro_name, "pro_id":pro_id},
            success: function (ret) {
                if (ret === 500) {
                    clearInterval(timer);
                }
                if (ret.data.length > 0) {
                    console.log("begin",begin)
                    begin++;
                    ret.data.forEach(function (line) {
                        $('#log').append(line + '<br />');
                    });
                    //$('#log').scrollHeight
                    var div = document.getElementById('log');
                    div.scrollTop = div.scrollHeight;
                    //div.style.backgroundColor='black'

                   //window.scrollTo(0, div.scrollHeight);
                }
                else{
                    $('#log').append(+'server error !!<br />');
                    clearInterval(timer)
                }
            }, error: function (ret) {
                $('#log').append(+'server error !!<br />');
                clearInterval(timer);
            }
        });
    }, 10000);
})